package com.yujn.order.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.yujn.order.bean.DynamicPerson2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@RefreshScope
@Component
public class Task1 implements ApplicationListener<RefreshScopeRefreshedEvent> {

    @Autowired
    private DynamicPerson2 dynamicPerson2;

    /**
     * 触发@RefreshScope执行逻辑会导致@Scheduled定时任务失效
     *
     */
    @Scheduled(cron = "*/3 * * * * ?")
    public void run(){
        System.out.println(DateUtil.now() + " " + JSONUtil.toJsonStr(dynamicPerson2));
    }

    /**
     *  实现Spring事件监听器，监听 RefreshScopeRefreshedEvent事件，监听方法中进行一次定时方法的调用
     *  触发bean的生成
     * @param event
     */
    @Override
    public void onApplicationEvent(RefreshScopeRefreshedEvent event) {
        System.out.println(this.toString());
    }
}
